<?php
namespace frontend\models;
use yii\base\Model;
use common\models\FeedsModel;
class FeedForm extends Model{
    public $content;
    public $_lastError;
    public function rules() {
        return [
            ['content','required'],
            ['content','string','max'=>255],
        ];
    }
    
    public function attributeLabels() {
        return [
            'id'=>ID,
            'content'=>'内容',
        ];
    }
    public function getList(){
        $model=new FeedsModel();
        $res= $model->find()->limit(10)
                ->with('user')
                ->orderBy(['id'=>SORT_DESC])
                ->asArray()
                ->all();
        return $res?:[];//若有值，则返回$res,否则返回空数组
    }
    //保存成功，根据返回值来判是否创建成功
    public function create(){
        try{
            $model=new FeedsModel();
            $model->user_id= \Yii::$app->user->identity->id;
            $model->content= $this->content;
            $model->created_at= time();
            if(!$model->save()){
                throw new Exception('保存失败是！');
            }
            return true;
        } catch (yii\base\Exception $e){
            $this->_lastError=$e->getMessage();
            return false;
        }
    }
}

